<!-- 培训班基础信息 -->
<template>
  <div class="pxbInformation">
    <div class="pxbInformation-content">
      <div class="pxbInformation-content-form">
        <el-row :gutter="20">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm"
            :disabled="disabled">
            <el-card style="margin-bottom: 20px">
              <h2>培训信息</h2>
              <el-form-item label="培训单位" prop="organizationId">
                <el-select v-model="ruleForm.organizationId" @change="changeUnit" placeholder="请选择培训单位">
                  <el-option v-for="item in unitlist" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="地址" prop="address">
                <el-input v-model="ruleForm.address"></el-input>
              </el-form-item>
              <el-form-item label="培训班名称" prop="className" style="margin-bottom: 0">
                <el-input v-model="ruleForm.className"></el-input>
                <span style="color: #f56c6c; font-size: 12px;font-weight: 600; margin-left: 10px">* 建议和国家平台系统培训班名称一致</span>
              </el-form-item>
              <el-form-item label="培训对象" prop="targetStudents">
                <el-input v-model="ruleForm.targetStudents"></el-input>
              </el-form-item>
              <el-form-item label="计划人数" prop="plannedNumber">
                <el-input type="number" v-model="ruleForm.plannedNumber"></el-input>
              </el-form-item>
              <el-form-item label="培训方式" prop="trainingMode">
                <el-input v-model="ruleForm.trainingMode"></el-input>
              </el-form-item>
              <el-form-item label="培训类型" prop="trainingType">
                <!-- <el-input v-model="ruleForm.trainingType"></el-input> -->
                <el-radio v-model="ruleForm.trainingType" label="初训">初训</el-radio>
                <el-radio v-model="ruleForm.trainingType" label="复训">复训</el-radio>
              </el-form-item>
              <el-form-item label="工种" prop="workTypeId">
                <el-select v-model="ruleForm.workTypeId" placeholder="请选择">
                  <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="培训日期" prop="trainingDate">
                <el-date-picker v-model="trainingDate" type="daterange" @change="handleDateChange" range-separator="至"
                  start-placeholder="开始日期" end-placeholder="结束日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="培训地址" prop="trainingLocation">
                <el-input v-model="ruleForm.trainingLocation"></el-input>
              </el-form-item>
            </el-card>
            <el-card style="margin-bottom: 20px">
              <h2>班主任信息</h2>
              <el-form-item label="班主任性别" prop="headTeacherGender">
                <el-radio-group v-model="ruleForm.headTeacherGender">
                  <el-radio label="男">男</el-radio>
                  <el-radio label="女">女</el-radio>
                </el-radio-group>

              </el-form-item>
              <el-form-item label="班主任姓名" prop="headTeacherName">
                <el-input v-model="ruleForm.headTeacherName"></el-input>
              </el-form-item>
              <el-form-item label="班主任电话" prop="headTeacherPhone">
                <el-input v-model="ruleForm.headTeacherPhone"></el-input>
              </el-form-item>
              <el-form-item label="班主任照片" prop="headTeacherPhoto">
                <el-upload class="avatar-uploader" :action="server" accept=".jpg,.jepg,.png" :show-file-list="false"
                  :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                  <div v-if="ruleForm.headTeacherPhoto">
                    <img style="width: 150px; height: 150px;opacity: 0.5;background-color: #000"
                      class="el-upload-list__item-thumbnail avatar" v-if="ruleForm.headTeacherPhoto"
                      :src="ruleForm.headTeacherPhoto" />
                    <span class="el-upload-list__item-actions">
                      <span class="el-upload-list__item-preview" @click.stop="handlePictureCardPreview()">
                        <i class="el-icon-zoom-in"></i>
                      </span>
                    </span>
                  </div>

                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="班主任邮箱或微信" prop="headTeacherContact">
                <el-input v-model="ruleForm.headTeacherContact"></el-input>
              </el-form-item>
            </el-card>
            <el-card style="margin-bottom: 20px">
              <h2>审核</h2>
              <el-form-item label="审核人意见" prop="auditorOpinion">
                <el-input type="textarea" rows="3" v-model="ruleForm.auditorOpinion"></el-input>
              </el-form-item>
              <el-form-item label="审核时间" prop="auditTime">
                <el-date-picker v-model="ruleForm.auditTime" type="datetime" placeholder="选择审核时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="审核人签字" prop="auditorSignature">
                <signature v-model="ruleForm.auditorSignature"></signature>
              </el-form-item>
              <el-form-item label="批准人意见" prop="approverOpinion">
                <el-input type="textarea" rows="3" v-model="ruleForm.approverOpinion"></el-input>
              </el-form-item>
              <el-form-item label="批准时间" prop="approvalTime">
                <el-date-picker v-model="ruleForm.approvalTime" type="datetime" placeholder="选择批准时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="批准人签字" prop="approverSignature">
                <signature v-model="ruleForm.approverSignature"></signature>
              </el-form-item>
            </el-card>
          </el-form>
        </el-row>
      </div>
    </div>
    <div class="pxbInformation-title">
      <div class="pxbInformation-title-right">
        <!-- <el-button v-if="!disabled" @click="resetForm('ruleForm')"
          >重置</el-button
        > -->
        <el-button v-if="!disabled" type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <!-- <el-button type="danger" @click="goBack">返回</el-button> -->
      </div>
    </div>
    <el-dialog :visible.sync="imgVisible" top="20px">
      <img width="100%" :src="ruleForm.headTeacherPhoto" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchDetails,
  fetchAdd,
  fetchUpdata,
  fetchUnit,
  fetchOptions
} from "@/api/OneStageOneGear/OneStageOneGear.js";
import signature from "@/components/signature/index.vue";
import { loadOptions } from "@babel/core";
function convertDate(dateStr) {
  const date = new Date(dateStr);
  const year = date.getFullYear();
  // getMonth() 返回的月份是从0开始的，因此需要加1
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();

  // 返回格式化的年月日时
  return `${year}-${month.toString().padStart(2, "0")}-${day
    .toString()
    .padStart(2, "0")} ${hour.toString().padStart(2, "0")}:00`;
}
export default {
  name: "PxbInformation",
  watch: {},
  data() {
    return {
      imgVisible: false,
      server: `${process.env.VUE_APP_BASE_API}/common/api/file/upload`,
      ruleForm: {
        organizationName: "",
        organizationId: "",
        address: "",
        className: "",
        targetStudents: "",
        plannedNumber: "",
        trainingMode: "",
        trainingType: "",
        workTypeId: "",
        trainingStartDate: "",
        trainingEndDate: "",
        trainingLocation: "",
        headTeacherName: "",
        headTeacherGender: "",
        headTeacherPhoto: "",
        headTeacherPhone: "",
        headTeacherContact: "",
        auditorOpinion: "",
        auditTime: "",
        auditorSignature: "",
        approverOpinion: "",
        approvalTime: "",
        approverSignature: "",
      },
      trainingDate: [],
      rules: {
        organizationId: [
          { required: true, message: "请输入培训单位", trigger: "blur" },
        ],
        className: [
          { required: true, message: "请输入培训名称", trigger: "blur" },
        ],
        address: [{ required: true, message: "请输入地址", trigger: "blur" }],
        targetStudents: [
          { required: true, message: "请输入培训对象", trigger: "blur" },
        ],
        plannedNumber: [
          { required: true, message: "请输入计划人数", trigger: "blur" },
        ],
        trainingMode: [
          { required: true, message: "请输入培训方式", trigger: "blur" },
        ],
        trainingLocation: [
          { required: true, message: "请输入培训地址", trigger: "blur" },
        ],
        headTeacherName: [
          { required: true, message: "请输入班主任姓名", trigger: "blur" },
        ],
        headTeacherGender: [
          { required: true, message: "请选择", trigger: "blur" },
        ],
        headTeacherPhone: [
          { required: true, message: "请输入班主任电话", trigger: "blur" },
        ],
        headTeacherContact: [
          { required: true, message: "请输入班主任邮箱", trigger: "blur" },
        ],
        // auditorOpinion: [
        //   { required: true, message: "请输入审核人意见", trigger: "blur" },
        // ],
        // auditorSignature: [
        //   { required: true, message: "请输入审核人意见", trigger: "blur" },
        // ],
        // approverOpinion: [
        //   { required: true, message: "请输入审核人意见", trigger: "blur" },
        // ],
        trainingType: [
          { required: true, message: "请选择培训类型", trigger: "change" },
        ],
        workTypeId: [
          { required: true, message: "请选择工种", trigger: "change" },
        ],
      },
      start: this.$route.query.Type,
      id: this.$route.query.Id,
      unitlist: [], //单位列表
      options: [],
      disabled: false, //是否可编辑
    };
  },
  components: {
    signature,
  },
  created() {
    console.log(this.$route.query);
    this.getUnit();
    this.getOptions()
    if (this.start == "2025010120250102" || this.start == "202501012025013") {
      this.details();
    }
    if (this.start == "2025010120250102") {
      this.disabled = true;
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    handlePictureCardPreview(file) {
      this.imgVisible = true;
    },
    handleAvatarSuccess(res, file) {
      // this.ruleForm.headTeacherPhoto = URL.createObjectURL(file.raw);
      this.ruleForm.headTeacherPhoto = res.data.url;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //单位数据
    async getUnit() {
      let res = await fetchUnit();
      console.log(res);
      if (res.code == 200) {
        this.unitlist = res.data;
        console.log(this.unitlist);
      }
    },
    // 工种数据
    async getOptions() {
      let res = await fetchOptions();
      console.log(res, '工种');
      if (res.code == 200) {
        this.options = res.data;

      }
    },
    //单位选择
    changeUnit(val) {
      console.log(val);
      this.unitlist.forEach((item) => {
        if (item.id == val) {
          this.ruleForm.organizationName = item.name;
        }
      })
    },
    handleDateChange(val) {
      console.log(val);
      this.ruleForm.trainingStartDate = convertDate(val[0]);
      this.ruleForm.trainingEndDate = convertDate(val[1]);
    },
    async details() {
      let res = await fetchDetails(this.id);
      console.log(res);
      if (res.code == 200) {
        this.ruleForm = res.data;
        this.trainingDate.push(
          res.data.trainingStartDate,
          res.data.trainingEndDate
        );
        console.log(this.trainingDate, "this.trainingDate");
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        console.log(valid);
        if (valid) {
          let res;
          if (this.ruleForm.id != "" && this.ruleForm.id) {
            res = await fetchUpdata(this.ruleForm);
          } else {
            res = await  fetchAdd(this.ruleForm);
          }
          console.log(res);
          if (res.code == 200) {
           
              this.$message({
                message: "提交成功",
                type: "success",
              });
              this.$router.go(-1);
          
          } 
        } else {
          alert("请填写完成信息!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.pxbInformation {
  width: 100%;
  height: 100%;
  background: #fff;
  padding: 20px 50px;
  box-sizing: border-box;

  .pxbInformation-title {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    margin-bottom: 20px;

    .pxbInformation-title-left {
      display: flex;
      align-items: center;

      .pxbInformation-title-left-img {
        width: 20px;
        height: 20px;
        margin-right: 10px;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .pxbInformation-title-left-text {
        font-size: 16px;
        font-weight: 600;
      }
    }
  }

  .pxbInformation-content {}
}

.el-card {
  border-radius: 20px;
}

/deep/ .avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    height: 50px;
    line-height: 50px;
  }

  .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 50px;
    line-height: 50px;
    text-align: center;
  }

  .avatar {
    width: 100px !important;
    height: 50px !important;
    display: block;
  }
}
</style>
